/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.warpper;

import cn.iot.card.open.constants.Constants;
import cn.iot.card.open.constants.factory.ConstantFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.List;

/**
 * 物联卡综合信息，智能网语音信息包装类
 *
 * @param <T>
 * @author zhanlele
 * @date 20200319 14:33:10
 * @modify 20200319 zhanlele v1.1.2 创建
 * @since v1.1.2
 */
public class SimInfoForVoiceWrapper<T> {
    private Page<T> page;
    private List<T> resultList;

    public SimInfoForVoiceWrapper(Page<T> page) {
        if (null != page && null != page.getRecords()) {
            this.page = page;
            this.resultList = page.getRecords();
        }
    }

    public Page<T> wrap(String[] fieldNames) throws Exception {
        if (null != resultList) {
            Iterator<T> iterator = resultList.iterator();
            while (iterator.hasNext()) {
                T t = iterator.next();
                wrapTheObject(t, fieldNames);
            }
        }

        return this.page;
    }

    private void wrapTheObject(T t, String[] fieldNames) throws Exception {
        for (String fieldName : fieldNames) {
            Field field = t.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            String fieldVal = (String) field.get(t);

            if (Constants.CHANGE_DATE.equals(fieldName)) {
                LocalDateTime localDateTime = LocalDateTime.parse(fieldVal,
                        DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT_SEC));
                String dateTime = localDateTime.format(DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT_NORMAL));
                field.set(t, dateTime);
            } else if (Constants.WHITE_STATUS.equals(fieldName)) {
                field.set(t, ConstantFactory.me().getSmartVoiceWhiteStatus(fieldVal));
            } else if (Constants.CALLED_LOCK.equals(fieldName)) {
                field.set(t, ConstantFactory.me().getSmartVoiceLockStatus(fieldVal));
            } else if (Constants.LIMIT_SIGN.equals(fieldName)) {
                field.set(t, ConstantFactory.me().getSmartVoiceLimitStatus(fieldVal));
            } else if (Constants.RESPONSE_STATUS.equals(fieldName)) {
                field.set(t, ConstantFactory.me().getGroupVoiceWhiteStatusName(fieldVal));
            }
        }
    }
}
